﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using template_dll;

namespace ServerClient
{
    public partial class ClientsForm : Form
    {
        public ClientsForm()
        {
            InitializeComponent();
        }

        private SqlConnection con = new SqlConnection(@"Server=sdata2;Database=ExchangeFilesDB;Integrated Security=True");

        private void button1_Click(object sender, EventArgs e)
        {
            ClientsSel();
        }

        public void ClientsSel()
        {
            
            try
            {
                int countParams = 1;
                if (textBox1.Text != "") countParams = 2;
                SPparams[] sp = new SPparams[countParams];
                sp[0] = new SPparams() { Name = "@closed", Value = checkBox1.Checked };
                if (sp.Length == 2) sp[1] = new SPparams() { Name = "@comments", Value = textBox1.Text };
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = new SqlStorProc().SqlProcExecWithQueryOneTable(con, "ClientsSel", 30, sp).DefaultView;
                dataGridView1.Columns[6].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State != ConnectionState.Closed) con.Close();
            }
        }

        private void ClientsForm_Load(object sender, EventArgs e)
        {
            ClientsSel();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                try
                {
                    DialogResult dr;
                    if (Convert.ToBoolean(dataGridView1[6, e.RowIndex].Value))
                        dr = MessageBox.Show("Открыть клиента " + dataGridView1[2, e.RowIndex].Value + "?", "Изменение статуса клиента", MessageBoxButtons.YesNo);
                    else
                        dr = MessageBox.Show("Закрыть клиента " + dataGridView1[2, e.RowIndex].Value + "?", "Изменение статуса клиента", MessageBoxButtons.YesNo);
                    if (dr == DialogResult.Yes)
                    {
                        bool closed = false;
                        if (!Convert.ToBoolean(dataGridView1[6, e.RowIndex].Value)) closed = true;
                        SPparams[] sp = new SPparams[2];
                        sp[0] = new SPparams() { Name = "@closed", Value = closed };
                        sp[1] = new SPparams() { Name = "@clientid", Value = dataGridView1[0, e.RowIndex].Value };
                        new SqlStorProc().SqlProcExecNonQuery(con, "ClientClosedUpd", 30, sp);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    ClientsSel();
                }
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) ClientsSel();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            ClientsSel();
        }


    }
}
